#include "wontec_wtais42.h"
#include "stdio.h"


char wtais_cmds[MAX_CMD_NUM_AIS] = 
{
    'A',
    'a',
    'B',
    'b',
    'C',
    'c',
    'D',
    'd',
    'E',
    'e',
    'f',
    'G',
    'g',
    'H',
    'h',
    'I',
    'i',
    'J',
    'j',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'R',
    'r',
    'S',
    'T',
    'U',
    'w',
    'X',
    'Y',
    'y',
    '+',
    '-',
    '*',
    '/',
};


char wtej28_cmds[32] = 
{
    'A',
    'a',
    'B',
    'C',
    'c',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'r',
    'S',
    'T',
    't',
    'U',
    'V',
    'v',
    'W',
    'w',    
    'X',
    'Y',
    'Z',
    'd',
};

void set_wtais_data(unsigned short addr,wontec_cmd *pstWtCmd,unsigned char * buffOut, unsigned char* lenOut)
{
    wontec_485_frame frame;
    
    frame.func_code = wtais_cmds[pstWtCmd->func_code];
    frame.slave_addrH = (addr>>8)&0x00ff;
    frame.slave_addrL = addr&0x00ff;
    frame.data = pstWtCmd->data;
    frame.dataLen = pstWtCmd->datalen;


    if(frame.slave_addrH >= 0 && frame.slave_addrH <= 9)
    {
        frame.slave_addrH += '0'; 
    }
    else if(frame.slave_addrH >= 0x0a && frame.slave_addrH <= 0x0f )
    {
        frame.slave_addrH += 'A'; 
    }

    if(frame.slave_addrL >= 0 && frame.slave_addrL <= 9)
    {
        frame.slave_addrL += '0'; 
    }
    else if(frame.slave_addrL >= 0x0a && frame.slave_addrL <= 0x0f )
    {
        frame.slave_addrL += 'A'; 
    }
    

    //memset(buffOut,0,MAX_BUFF_LEN);
    printf("func code:%c\n",frame.func_code);

    wontec_frame_serialize(&frame,buffOut,lenOut);
}

void set_wtej28_data(unsigned short addr,wontec_cmd *pstWtCmd,unsigned char * buffOut, unsigned char* lenOut)
{
    wontec_485_frame frame;
    
    frame.func_code = wtej28_cmds[pstWtCmd->func_code];
    frame.slave_addrH = (addr>>8)&0x00ff;
    frame.slave_addrL = addr&0x00ff;
    frame.data = pstWtCmd->data;
    frame.dataLen = pstWtCmd->datalen;

    if(frame.slave_addrH >= 0 && frame.slave_addrH <= 9)
    {
        frame.slave_addrH += '0'; 
    }
    else if(frame.slave_addrH >= 0x0a && frame.slave_addrH <= 0x0f )
    {
        frame.slave_addrH += 'A'; 
    }

    if(frame.slave_addrL >= 0 && frame.slave_addrL <= 9)
    {
        frame.slave_addrL += '0'; 
    }
    else if(frame.slave_addrL >= 0x0a && frame.slave_addrL <= 0x0f )
    {
        frame.slave_addrL += 'A'; 
    }

    //memset(buffOut,0,MAX_BUFF_LEN);
    printf("func code:%c\n",frame.func_code);

    wontec_frame_serialize(&frame,buffOut,lenOut);
}


int checkProcWtej28OnPoint(void * dataStd,void * dataRecv)
{
   wontec_485_frame * ptrStd = (wontec_485_frame *)dataStd; 
   wontec_485_frame * ptrRecv = (wontec_485_frame *)dataRecv;

   int checkRet = -1;
   printf("enter in checkProcWtej28OnPoint,ptrRecv:%08x,ptrStd:%08x\n",ptrRecv,ptrStd);
   if(ptrStd==NULL || ptrRecv==NULL)
   {
       return checkRet;
   }

   if( ptrStd->func_code == ptrRecv->func_code
     &&ptrStd->slave_addrH == ptrRecv->slave_addrH
     &&ptrStd->slave_addrL == ptrRecv->slave_addrL )
   {
       if(ptrRecv->dataLen >= 2)
       {
           if(ptrRecv->data[0] == ptrStd->data[0]
           &&ptrRecv->data[1] == ptrStd->data[1])
           {
              checkRet = 0;
           }
       }
   }

   return checkRet;

}